package com.wangbo.multi.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Package: cn.stylefeng.guns.modular.fabric.util
 * @Author: Mr.Waves
 * @Date: 2020-04-15 10:54
 * @Description: 转换工具类
 **/
@UtilityClass
public class ConvertUtil {

    /**
     * S -> T
     */
    @SneakyThrows
    public <S, T> T convert(S source, Class<T> clz) {
        T target = clz.newInstance();
        BeanUtil.copyProperties(source, target, CopyOptions.create().ignoreNullValue().ignoreError());
        return target;
    }

    /**
     * List<S> -> List<T>
     */
    public <S, T> List<T> convert(List<S> source, Class<T> clz) {
        return source.stream().map(sourceElement -> convert(sourceElement, clz)).collect(Collectors.toList());
    }


}
